package com.kgc.ebook02.controller;

import com.github.pagehelper.PageInfo;
import com.kgc.ebook02.pojo.Category;
import com.kgc.ebook02.pojo.Entry;
import com.kgc.ebook02.pojo.ResultEntity;
import com.kgc.ebook02.service.EntryService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
public class EntryController {
    @Resource
    EntryService entryService;

    //查询Entry的所有信息
    @RequestMapping("/selectEntry")
    public ResultEntity<PageInfo<Entry>> selectEntry(@RequestParam(value = "pageNum",defaultValue ="1") Integer pageNum,
                                                 @RequestParam(value = "categoryid",defaultValue = "0")Integer categoryid
                                                     ){
        PageInfo<Entry> pageInfo=entryService.selectAll(pageNum,categoryid);
        return ResultEntity.successWithoutData(pageInfo);
    }


    //查询Category的所有信息
    @RequestMapping("/getCategoryList")
    public ResultEntity<List<Category>> getCategoryList(){
        List<Category> categories = entryService.selectAllCategory();
        return ResultEntity.successWithoutData(categories);
    }
    //添加图书信息
    //和修改
    @RequestMapping("/saveEntry")
    public  ResultEntity<String> saveEntry(@RequestBody Entry entry){
        try {
            Integer saveEntry=0;
            if (entry.getId()==null){
                saveEntry= entryService.saveEntry(entry);
            }else{
                saveEntry=  entryService.updateEntry(entry);
            }
            return ResultEntity.successWithoutData(saveEntry+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }


    }

    //根据id删除信息
    @RequestMapping("/delEntryById")
    public  ResultEntity<String> delEntryById(Integer id){
        try {
            Integer deleteEntry = entryService.deleteEntry(id);
            return ResultEntity.successWithoutData(deleteEntry+"");
        }catch (Exception ex){
            return ResultEntity.failed(ex.getMessage());
        }


    }
    //根据id查询信息
    @RequestMapping("/getEntryById")
    public  ResultEntity<Entry> getEntryById(Integer id){
        Entry entry = entryService.selectAllByEntry(id);
        return ResultEntity.successWithoutData(entry);
    }
}
